package com.example.spark.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.spark.entity.Direction;
import com.example.spark.entity.result.CommonResult;
import com.example.spark.service.DirectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/direction")
public class DirectionController {

    @Autowired
    private DirectionService directionService;

    //根据方向id查询方向
    @GetMapping("selectDirection/{id}")
    public CommonResult selectById(@PathVariable Long id) {

        return directionService.selectById(id);
    }

    //新增学习方向
    @PostMapping("addDirection")
    public CommonResult addDirection(@RequestBody Direction direction) {

        return directionService.addDirection(direction);
    }

    //修改学习方向
    @PutMapping("updateDirection")
    public CommonResult updateDirection(@RequestBody Direction direction) {

        return directionService.updateDirection(direction);
    }

    //删除学习方向
    @DeleteMapping("deleteDirection/{id}")
    public CommonResult deleteDirection(@PathVariable Long id) {

        return directionService.deleteDirection(id);
    }

    //列表
    @GetMapping("listDirection/{currentPage}/{pageSize}")
    public CommonResult selectList(@PathVariable int currentPage, @PathVariable int pageSize) {
        IPage<Direction> page = directionService.getPage(currentPage, pageSize);
        //如果当前页码值大于了总页码值,那么重新执行查询操作,使用最大页码值作为当前页码值
        if (currentPage > page.getPages()) {
            page = directionService.getPage((int) page.getPages(), pageSize);
        }
        return new CommonResult<>(200, "", page);
    }

    //图片
    @PutMapping("directionPhotos")
    public CommonResult addPhoto(@RequestParam MultipartFile photoFile, @RequestParam Long id) throws IOException {

        return directionService.addPhoto(photoFile, id);
    }

    @GetMapping("getDirection")
    public CommonResult getIdAndName() {

        return directionService.getIdAndName();
    }
}
